package com.mdd.front.service.course.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.course.CourseCategory;
import com.mdd.common.mapper.course.CourseCategoryMapper;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.front.service.course.ICourseCategoryService;
import com.mdd.front.vo.course.CourseCategoryListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 课程分类实现类
 * @author panweiliang
 */
@Service
public class CourseCategoryServiceImpl implements ICourseCategoryService {
        
    @Resource
    CourseCategoryMapper courseCategoryMapper;

    /**
     * 课程分类列表
     *
     * @author panweiliang
     * @return List<CourseCategoryListVo>
     */
    @Override
    public List<CourseCategoryListVo> list() {
        QueryWrapper<CourseCategory> queryWrapper = new QueryWrapper<CourseCategory>()
                .eq("status", "1")
                .eq("level", 1)
                .orderByAsc("create_time");
        List<CourseCategory> list = courseCategoryMapper.selectList(queryWrapper);
        List<CourseCategoryListVo> vos = new LinkedList<>();
        for(CourseCategory item : list) {
            CourseCategoryListVo vo = new CourseCategoryListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setImage(UrlUtil.toAbsoluteUrl(item.getImage()));
            //获取子分类
            QueryWrapper<CourseCategory> childrensQueryWrapper = new QueryWrapper<CourseCategory>()
                    .eq("pid", item.getId())
                    .eq("status", "1")
                    .orderByAsc("create_time");
            List<CourseCategory> childrens = courseCategoryMapper.selectList(childrensQueryWrapper);
            List<CourseCategoryListVo> childrenVos = new LinkedList<>();
            childrens.forEach(childrenItem -> {
                CourseCategoryListVo childrenVo = new CourseCategoryListVo();
                BeanUtils.copyProperties(childrenItem, childrenVo);
                childrenVo.setImage(UrlUtil.toAbsoluteUrl(childrenItem.getImage()));
                childrenVos.add(childrenVo);
            });
            vo.setChildrens(childrenVos);
            vos.add(vo);
        }
        return vos;
    }


}
